<?php
// +----------------------------------------------------------------------
// | MYUPHP [ Simple and practical framework ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-08-10 http://myuphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
namespace myuphp\Common;
require_once MYUPHP . '/Common/Common.php';
use myuphp\common\Tpl;
class Myuphp
{
    public static function pre($str)
    {
        $new = preg_replace("/<.*?>|<\\/>/is", "", $str);
        return $new;
    }
    // 获取get数据
    public static function get($get = 'c', $id = '')
    {
        if (!empty($_GET[$get])) {
            return Myuphp::pre($_GET[$get]);
        } else {
            return Myuphp::pre($id);
        }
    }
    //获取控制器文件
    public static function get_controller()
    {
        if (empty(Myuphp::get('s'))) {
            $new = '\\index';
        } else {
            $new = '\\'.get('s');
        }
        return $new;
    }
    //获取应用目录
    public static function get_app($c)
    {
        $new = preg_replace("/\\//is", "\\", $c);
        $new = preg_replace("/\\./is", "", $new);
        return $new;
    }
    public static function get_url($get = 'c', $id = '')
    {
        if (!empty($_GET[$get])) {
            return $_GET[$get];
        } else {
            return $id;
        }

 

    }
 
    //获取配置
    public static function os_config($myuphp)
    {
        $include = (include 'myuphp/config.php');
        $includes = (include PATH.'/config.php');
        if (empty($includes[$myuphp])) {
            return $include[$myuphp];
        } else {
            return $includes[$myuphp];
        }
    }
    public static function server()
    {
        $include = (include PATH.'/Route.php');
        $url= preg_replace('/\\'.Myuphp::os_config('path').'/is', '/', preg_replace('/\\/index.php|.html|\?(.*)/', '', $_SERVER['REQUEST_URI']));
        $preg= preg_replace('/\?/is', '/', $url);
        $arr = explode('/', $preg);
        if ($_SERVER['REQUEST_URI']=='/') {
            return '/index/index/index';
        } else {
            if (array_search($arr[1],$include)) {
                $route = array_search($arr[1],$include);
                return preg_replace('/\\/index.php|.html|\?(.*)/', '', '/'.$route);
            } else {
                return preg_replace('/\\/index.php|.html|\?(.*)/', '', $_SERVER['REQUEST_URI']);
            }
        }
    }
    //获取完整的url
    public static function curPageURL() 
    {
      $pageURL = 'http';
     
      if ($_SERVER["HTTPS"] == "on") 
      {
        $pageURL .= "s";
      }
      $pageURL .= "://";
      if ($_SERVER["SERVER_PORT"] != "80") 
      {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
      } 
      else
      {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
      }
      return $pageURL;
    }
    //p判断是否能打开文件
    public static function file($file){
        $take = new \myuphp\common\Tpl();
        if(!file_exists($file)){
             $take->error('控制器不存在');
        } else {
            return $file;
        }
    }
    //错误跳转
    public static function Jump($name,$v=''){
        $take = new \myuphp\common\Tpl();
        $take->error($name,$v);
    }

}